package com.neusoft.movie.v_movies.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.ArticlesModel;
import com.neusoft.movie.v_movies.service.ArticlesService;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.neusoft.movie.v_movies.common.Constants.BASE_URL;

public class AddArtActivity extends  BaseActivity implements RetrofitListener {

    private EditText a_name,a_info;
    private ImageView a_pic,left,btn_add_image;
    private int a_id;
    private ImageView button_add;
    private int u_id,author_id;
    private String aname,ainfo;
    String imagePath;
    public int IMAGE = 1;


    @Override
   void initViews() {
        setLayout(R.layout.activity_add_art);

        a_name = (EditText) findViewById(R.id.a_name);
        a_pic = (ImageView) findViewById(R.id.a_pic);
        a_info = (EditText) findViewById(R.id.a_info);

        button_add=(ImageView) findViewById(R.id.button_add);
        btn_add_image=(ImageView) findViewById(R.id.btn_add_image);
        left=(ImageView) findViewById(R.id.left_button);

   }

    @Override
    void initData() {
    }

    @Override
    void initEvents() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_add_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChoosePicDialog();
            }
        });
        button_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aname = a_name.getText().toString().trim();
                ainfo = a_info.getText().toString().trim();
                if (ainfo.equals("")||aname.equals("")) {
                    Toast.makeText(AddArtActivity.this, "请输入发帖信息", Toast.LENGTH_SHORT).show();
                } else {
                    upLoadPpicFile();
                }
            }
        });


    }
    //显示修改图片的对话框
    protected void showChoosePicDialog() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, IMAGE);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //获取图片路径
        if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();
            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            imagePath = c.getString(columnIndex);
            showImage(imagePath);
            c.close();
        }
    }
    private void showImage(String imaePath){
        Bitmap bm = BitmapFactory.decodeFile(imaePath);
        a_pic.setImageBitmap(bm);
    }


    private void upLoadPpicFile() {

        Retrofit retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ArticlesService articlesService = retrofit.create(ArticlesService.class);

        File file = new File(imagePath);
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        Call<ResponseBody> call = articlesService.addarticles(getUser_id(),aname,ainfo,body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call,
                                   Response<ResponseBody> response) {

                Toast.makeText(AddArtActivity.this, "发表成功", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("Upload error:", t.getMessage());
            }
        });
    }

    @Override
    public void onSuccess(Object o, int flag) {

    }

    @Override
    public void onFailed() {

    }
}

